package com.whoisaa.wifitransfer.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.whoisaa.wifitransfer.R;
import com.whoisaa.wifitransfer.bean.FileFolder;
import com.whoisaa.wifitransfer.utils.StringUtils;
import com.whoisaa.wifitransfer.widget.GridViewForScrollView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @Description (影音&图片&文档&应用&其他)列表适配器
 * @Author AA
 * @DateTime 16/4/22 下午3:53
 */
public class ChooseFileAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private List<FileFolder> mData;
    private List<String> mSelectedFiles = new ArrayList<>();
    private OnFileChooseListener mListener;


    public ChooseFileAdapter(Context context, List<FileFolder> data, OnFileChooseListener listener) {
        this.mContext = context;
        this.mData = data;
        this.mListener = listener;
    }


    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public int getGroupCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mData.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mData.get(groupPosition).getFilePaths();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder holder = null;
        if(convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_choose_file_group, parent, false);
            holder = new GroupViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (GroupViewHolder) convertView.getTag();
        }
        String dirName = ((FileFolder) getGroup(groupPosition)).getDirName();
        if(!StringUtils.isEmpty(dirName)) {
            holder.textView.setText(dirName);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder = null;
        if(convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_choose_file_child, parent, false);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }

        final String dirPath = ((FileFolder) getGroup(groupPosition)).getDirPath();
        final List<String> images = (List<String>) getChild(groupPosition, childPosition);
        childViewHolder.gridView.setAdapter(new CommonAdapter<String>((Activity) mContext, images, R.layout.item_choose_image) {
            @Override
            public void convert(ViewHolder holder, final String item) {
                final ImageView imageView = holder.getView(R.id.iv_item_choose_image);
                final ImageView statusView = holder.getView(R.id.iv_item_choose_status);

                //加载图片
                final String fullPath = dirPath + "/" + item;
                Glide.with(mContext).load(fullPath).diskCacheStrategy(DiskCacheStrategy.ALL).centerCrop().into(imageView);
                //选择图片后显示勾选图标并且变暗
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(mSelectedFiles.contains(fullPath)) {
                            //取消选中
                            mSelectedFiles.remove(fullPath);
                            statusView.setVisibility(View.GONE);
                            imageView.setColorFilter(null);

                            if(mListener != null) {
                                mListener.onUnselected(fullPath);
                            }
                        } else {
                            //选中
                            mSelectedFiles.add(fullPath);
                            statusView.setVisibility(View.VISIBLE);
                            imageView.setColorFilter(Color.parseColor("#77000000"));

                            if(mListener != null) {
                                mListener.onSelected(fullPath);
                            }
                        }
                    }
                });

                //勾选已选择过的图片
                if(mSelectedFiles.contains(fullPath)) {
                    statusView.setVisibility(View.VISIBLE);
                    imageView.setColorFilter(Color.parseColor("#77000000"));
                }
            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {

    }

    @Override
    public void onGroupCollapsed(int groupPosition) {

    }

    @Override
    public long getCombinedChildId(long groupId, long childId) {
        return 0;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return 0;
    }

    public class GroupViewHolder {

        public GroupViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

        @Bind(R.id.tv_item_choose_file_group)
        TextView textView;
    }

    public class ChildViewHolder {

        public ChildViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

        @Bind(R.id.gv_choose_file_child)
        GridViewForScrollView gridView;
    }

    public interface OnFileChooseListener {
        public void onSelected(String path);
        public void onUnselected(String path);
    }
}
